Create a Client Hello World! Script
Create a new script#
Expand your project in the extension's 'Project's, Scripts & Configs' tree. You will see there are now tree nodes for Scripts, User Configs, and API Configs. If you expand each you can see they are both empty.
- Right click on the Scripts node and click 'Create New Script'
- In the Create Script form
- For Script Name enter '01 - Hello World'
- For Script Description enter 'My First Script'
- For Script Short Name enter 'hw'
- For Script User Type enter 'hello-world'
- Leave the default script content for now, we will update it later
- Click the Save button
The Scripts node will then refresh to display the new script you have created.
The cloud icon next to your script's name indicates the script only exists on the Twinit platform, and you do not have it local to edit.
Open the script locally#
- Right click the '[v1] 01 - Hello World' script you just created, click Open, and it will open in a new window
Notice the cloud icon next to the script changed to a document icon indicating that you now have that script local for editing.
Understanding the script signature#
Look at the default sampleScript on line 2. Let's review the script signature so we can understand what parameters are passed to the scripts.
input - Any and all input to the script. This could be data passed by a client application, data passed in from a previous step in a datasources orchestrator, request data from an OMAPI endpoint, or any other input coming from the caller of the script
libraries - The various libraries provided to the script for interacting with the Twinit platform or the user. Currently these libraries are the PlatformApi, IafScriptEngine, and UiUtils modules for client scripts. For backend scripts, which we will learn about in the next section, the libraries are different.
ctx - Information such as the user's token, current project, _namespaces, and other contextual information. Every api call to Twinit, which uses any Iaf javascript API, must include a ctx.
callback - An optional callback function which clients can use to receive async status or data from the script. Not supported in backend scripts, such as those used for OMAPI and Orchestrator scripts.
Edit the script#
- Rename the default 'sampleScript' to 'helloWorld'
- In the script itself add a return statement:
let scriptModule = { async helloWorld(input, libraries, ctx, callback) { return 'Hello World!' },}
export default scriptModule- Then save the script locally (Ctrl-S or File > Save)
Notice the document icon next to the script has changed to a triangular alert icon. This indicates that your local copy of the script differs from the version of the script on the Twinit platform, because you have changed it locally.
Run the script#
- Right click on the '01 - Hello World' script and click 'Run Script'
- When asked to select a script to run at the top of the IDE window select 'helloWorld'
While the script is running you will see a status indicator in the bottom left hand corner of the IDE status bar. When the script has completed running results of the script will display.
Understanding script results#
Review the newly opened results file. The results contain these properties:
script - The name of the script which you ran
run - When you ran the script
elapsed - How long it took the script to complete
status - Whether the script succeeded in executing or errored
result - What was returned by the script. The contents of result are what the script return on completion